
import java.io.InputStream;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class VelhaMIDlet extends MIDlet
        implements CommandListener {

    public static VelhaMIDlet instance;
    private Form log = new Form("Log");
    private List menu;
    private List bt;
    private TextBox ip;
    private TextBox nome;
    private Command ok;
    private int pl = 0;

    public VelhaMIDlet() {
        instance = this;
        this.menu = new List("Menu", 3);
        this.menu.append("Jogador 1", null);
        this.menu.append("Jogador 2", null);
        //this.menu.append("Enviar", null);
        this.menu.setCommandListener(this);

        this.ip = new TextBox("Endereço IP", null, 100, 0);
        this.ok = new Command("Conectar", 4, 0);
        this.ip.addCommand(this.ok);
        this.ip.setCommandListener(this);

        this.nome = new TextBox("Apelido", null, 30, 0);
        this.nome.addCommand(this.ok);
        this.nome.setCommandListener(this);
    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
    }

    protected void startApp() throws MIDletStateChangeException {
        Splash s = new Splash();
        getDisplay().setCurrent(s);
        s.play();
        getDisplay().setCurrent(this.menu);
    }

    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    public void commandAction(Command c, Displayable d) {
        if ((d == this.menu) && (c.equals(List.SELECT_COMMAND))) {
            int index = this.menu.getSelectedIndex();
            
            this.pl = (index + 1);
            getDisplay().setCurrent(this.nome);
           
        } else if ((d == this.ip) && (c.equals(this.ok))) {
            getDisplay().setCurrent(this.log);
            new Thread() {
                public void run() {
                    try {
                        VelhaMIDlet.this.getDisplay().setCurrent(VelhaMIDlet.this.log);
                        Game g = PlayTwo.start(VelhaMIDlet.this.log, VelhaMIDlet.this.ip.getString(), "65432", VelhaMIDlet.this.nome.getString());
                        VelhaMIDlet.this.getDisplay().setCurrent(g);
                        g.start();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
                    .start();
        } else if ((d == this.nome) && (c.equals(this.ok))) {
            if (this.pl == 1) {
                getDisplay().setCurrent(this.log);

                new Thread() {
                    public void run() {
                        try {
                            Game g = PlayOne.start(VelhaMIDlet.this.log, "65432", VelhaMIDlet.this.nome.getString());
                            VelhaMIDlet.this.getDisplay().setCurrent(g);
                            g.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                        .start();
            } else {
                getDisplay().setCurrent(this.ip);
            }
        }
    }
}
